package com.supertange.miniapp.wepet.modules.wepet.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.supertange.miniapp.wepet.common.vo.AjaxResult;
import com.supertange.miniapp.wepet.modules.wepet.entity.vo.ExchangeVO;
import com.supertange.miniapp.wepet.modules.wepet.service.ExchangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/exchange")
public class ExchangeController {

    @Autowired
    private ExchangeService exchangeService;

    @GetMapping("/queryGoods")
    public AjaxResult queryGoodsAll() {
        return AjaxResult.ok(exchangeService.queryAllGoods());
    }

    @PostMapping("/queryExchagneInfo")
    public AjaxResult queryExchangeInfo(String userid) {
        ExchangeVO exchangeVO = exchangeService.queryExchangeInfo(userid);
        if(exchangeVO==null){
            return AjaxResult.errorMsg("用户信息不对");
        }else{
            return AjaxResult.ok(exchangeVO);
        }
    }

    @PostMapping("queryExchangeRecordByUserid")
    public AjaxResult queryExchangeRecordByUserid(String userid){
        return AjaxResult.ok(exchangeService.queryExchangeRecordByUserid(userid));
    }

    @PostMapping("/exchangeYisen")
    public AjaxResult exchangeYisen(String userid,int favourate_cnt) {
       if(!exchangeService.addYisen(userid,favourate_cnt))
           return AjaxResult.errorMsg("兑换失败");
       else
           return AjaxResult.ok();
    }



    @PostMapping("/addGoods")
    public AjaxResult addGoods(String image_url,int money_count,int cost_count) {
        exchangeService.addGoods(image_url,money_count,cost_count);
        return AjaxResult.ok();
    }


    @PostMapping("/addOrder")
    public AjaxResult addOrder(String kuaidi_address,String kuaidi_name,String kuaidi_tel,
                               String kuaidi_message,String goodsid,String userid,int goods_cnt,
                               String list_addr,String list_name,String list_tel,String list_kucun) {
        int res = exchangeService.addOrder(kuaidi_address,
                kuaidi_name,kuaidi_tel,kuaidi_message,goodsid,
                userid,goods_cnt,list_addr,list_name,list_tel,list_kucun);
        switch (res){
            case 0:
                return AjaxResult.ok();
            case -1:
                return AjaxResult.errorMsg("未找到用户");
            case -2:
                return AjaxResult.errorMsg("未找到商品");
            case -3:
                return  AjaxResult.errorMsg("用户一森币不够");

        }
        return AjaxResult.errorMsg("异常");
    }

    @PostMapping("/addUserAddr")
    public AjaxResult addUserAddr(String list_name,String list_tel,String list_addr,String userid){
        if(exchangeService.addAddr(list_name,list_tel,list_addr,userid))
            return AjaxResult.ok();
        else
            return AjaxResult.errorMsg("修改失败");
    }

}
